package com.yupi.project.service.impl;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yupi.project.common.ErrorCode;
import com.yupi.project.constant.UserConstant;
import com.yupi.project.exception.BusinessException;
import com.yupi.project.mapper.UserMapper;
import com.yupi.project.model.domain.User;
import com.yupi.project.service.UserService;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.servlet.http.HttpServletRequest;

/**
* @author dell
* @description 针对表【user(用户)】的数据库操作Service实现
* @createDate 2024-04-21 22:55:04
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService {

    @Autowired
    private UserMapper userMapper;

    // 密码加密盐值
    private static final String  SALT  = "SJB";

    @Override
    public long userRegister(String userAccount, String userPassword, String checkPassword) {
        if (StringUtils.isEmpty(userAccount)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        if (userPassword.length() < 4 ){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"密码过短");
        }
        if (!userPassword.equals(checkPassword)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"两次输入的密码不一致");
        }
        // 校验账户是否重复
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        QueryWrapper<User> selectUserAccount = userQueryWrapper.eq("userAccount", userAccount);
        Long selectCount = userMapper.selectCount(selectUserAccount);
        if (selectCount > 0){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"账号已存在");
        }
        // 对密码处理 加密
        String encryptPassword = DigestUtils.md5DigestAsHex((SALT + userPassword).getBytes());
        String accessKey = DigestUtil.md5Hex(SALT + userAccount + RandomUtil.randomNumbers(8));
        String secret = DigestUtil.md5Hex(SALT + userAccount + RandomUtil.randomNumbers(5));

        // 插入数据
        User user = new User();
        user.setUseraccount(userAccount);
        user.setUserpassword(encryptPassword);
        user.setAccesskey(accessKey);
        user.setSecretkey(secret);
        boolean result = this.save(user);
        if (!result){
            throw new BusinessException(ErrorCode.DATABASE_ERROR,"保存数据失败");
        }
        return user.getId();
    }

    @Override
    public User userLogin(String userAccount, String userPassword, HttpServletRequest request) {
        if (StringUtils.isEmpty(userAccount) || StringUtils.isEmpty(userPassword)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"请求参数错误");
        }
        String encryptPassword = DigestUtil.md5Hex((SALT + userPassword).getBytes());
        QueryWrapper<User> userByWrapper = new QueryWrapper<User>()
                .eq("userAccount", userAccount)
                .eq("userPassword",encryptPassword);
        User user = userMapper.selectOne(userByWrapper);
        if (user == null){
            throw new BusinessException(ErrorCode.USER_NOT_EXIST_ERROR,"用户不存在");
        }
        // 记录用户的登录态信息
        request.getSession().setAttribute(UserConstant.USER_LOGIN_STATE,user);
        return user;
    }
}




